<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container" >
      <el-input
        v-model="infoForm.userId"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="请输入学号"
      />
      <el-input
        v-model="infoForm.bookName"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="请输入书名"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="queryList(1)"
      >查找</el-button>
    </div>

    <!-- 查询结果 -->
    <div>
      <el-table
        v-loading="listLoading"
        :data="list"
        size="small"
        element-loading-text="正在查询中。。。"
        :height="tabHeight"
        fit
        highlight-current-row
      >
        <el-table-column align="center" width="200px" label="学号" prop="userId"/>
        <el-table-column align="center" width="200px" label="姓名" prop="userName"/>
        <el-table-column align="center" width="200px" label="专业" prop="specialized"/>
        <el-table-column align="center" width="200px" label="书名" prop="bookName"/>
        <el-table-column align="center" width="200px" label="用户预约时间" prop="startTime"/>
        <el-table-column
          align="center"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addBook(scope.row)">确定借阅</el-button>
            <el-button type="text" size="small" @click="deleteBook(scope.row)">取消借阅</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <!-- 分页角标-->
    <div class="fypage">
      <el-pagination
        v-show="total>0"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

  </div>
</template>

<script>
    export default {
        name: "borrowing",
        data(){
            return{
                infoForm: {
                    userId:'',
                    bookName:'',
                },
                list:[],
                total: 1,
                pageNum: 1,
                pageSize: 10,
                orderBy: '',
                sort: '',
                listLoading: false,
                tabHeight: 540,
            }
        },

        created() {
            this.queryList(1)
        },

        methods: {

            //取消预约
            deleteBook(row){
                this.$confirm(' ' + row.userName + '的预约将被取消, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let self = this
                    self.listLoading = true
                    self.$axios.get("/manage/borrow/updateBorrowingState",{
                        params:{
                            userId: row.userId,
                            bookId: row.bookId,
                            borrowingState: 1
                        }
                    }).then(res =>{
                        try {
                            self.list = res.data.data
                            self.total = res.data.total
                            self.listLoading = false
                        }catch(err){
                            self.listLoading = false
                            self.$message.error(res.data.msg)
                        }
                    }).catch(err => {
                        self.$message.error("查询出错！！！！！")
                        self.listLoading = false
                    })

                }).catch(() => {
                })
            },

            //立即预约
            addBook(row){
                this.$confirm(' ' + row.userName + '的预约将立即生效, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let self = this
                    self.listLoading = true
                    self.$axios.get("/manage/borrow/updateBorrowingState",{
                        params:{
                            userId: row.userId,
                            bookId: row.bookId,
                            borrowingState: 0
                        }
                    }).then(res =>{
                        try {
                            self.list = res.data.data
                            self.total = res.data.total
                            self.listLoading = false
                        }catch(err){
                            self.listLoading = false
                            self.$message.error(res.data.msg)
                        }
                    }).catch(err => {
                        self.$message.error("查询出错！！！！！")
                        self.listLoading = false
                    })
                }).catch(() => {
                })
            },

            handleSizeChange(size){
                this.queryList()
            },
            handleCurrentChange(current){
                this.queryList(current)
            },

            //当前页数，排序字段，排序方式
            queryList(page,order,sortord){
                const self = this
                self.listLoading = true
                self.pageNum = page
                self.orderBy = order
                self.sort = sortord
                let data = {
                    'bookName':self.infoForm.bookName || null, //图书名
                    'author':self.infoForm.userId || null, //学号

                    'orderBy': self.orderBy || null, //排序字段
                    'pageNum': self.pageNum || 1, //当前页数
                    'pageSize': self.pageSize || 10, //每页展示条数
                    'sort': self.sort || null, //排序方式
                }
                self.$axios.post('/manage/borrow/borrowingUser',data).then(res =>{
                    try {
                        self.list = res.data.data
                        self.total = res.data.total
                        self.listLoading = false
                    }catch(err){
                        self.listLoading = false
                        self.$message.error(res.data.msg)
                    }
                }).catch(err => {
                    self.$message.error("查询出错！！！！！")
                    self.listLoading = false
                })
            },
        }
    }
</script>

<style scoped>

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }

</style>

